package models;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;

import play.db.jpa.Model;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;

//@Entity
public class SensorData extends Model {

    private static final long serialVersionUID = 1L;

    private long timestamp;
    private int sensorValue;
    private String sensorUID;
    private String hostUID;
    private TFSensorType sensorType;

    protected SensorData() {
    }

    public SensorData(long timestamp, int sensorValue, String sensorUID, String hostUID, TFSensorType sensorType) {
        this.timestamp = timestamp;
        this.sensorValue = sensorValue;
        this.sensorUID = sensorUID;
        this.hostUID = hostUID;
        this.sensorType = sensorType;
    }

    public SensorData(int sensorValue, String sensorUID, String hostUID, TFSensorType sensorType) {
        this(new Date().getTime(), sensorValue, sensorUID, hostUID, sensorType);
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getSensorValue() {
        return sensorValue;
    }

    public void setSensorValue(int sensorValue) {
        this.sensorValue = sensorValue;
    }

    public String getSensorUID() {
        return sensorUID;
    }

    public void setSensorUID(String sensorUID) {
        this.sensorUID = sensorUID;
    }

    public TFSensorType getSensorType() {
        return sensorType;
    }

    public void setSensorType(TFSensorType sensorType) {
        this.sensorType = sensorType;
    }

    public String getHostUID() {
        return hostUID;
    }

    public void setHostUID(String hostUID) {
        this.hostUID = hostUID;
    }

//    public static List<SensorData> getObsoleteSensorDatas(long amountRemainingEntries) {
//        return find("SELECT c FROM SensorData c WHERE c.id <= ((SELECT MAX(s.id) FROM SensorData s)-?1)",
//                amountRemainingEntries).fetch();
//    }
//
//    public static SensorData getLastMeasurement(String uid) {
//        return getLastMeasurements(uid, 1).get(0);
//    }
//
//    public static List<SensorData> getLastMeasurements(String uid, int count) {
//        return find("SELECT c FROM SensorData c WHERE c.sensorUID=?1 ORDER BY c.timestamp DESC", uid).fetch(count);
//    }
//
//    public static List<Integer> getLastMeasurementsSensorValues(String uid, int count) {
//        return find("SELECT c.sensorValue FROM SensorData c WHERE c.sensorUID=?1 ORDER BY c.timestamp DESC", uid)
//                .fetch(count);
//    }
//
//    public static List<Integer> getLastFiveMeasurements(String uid) {
//        return getLastMeasurementsSensorValues(uid, 5);
//    }

    public String toString() {
        return "SensorData: {Timestamp=" + new Timestamp(timestamp) + " value=" + sensorValue + " UID=" + sensorUID
                + "}";
    }

}
